package com.bbf.springbootdemo.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.Charset;
import java.util.regex.Pattern;

/**
 * 基础工具类接口
 * @author BBF
 */
public final class BaseUtil {
  private BaseUtil() {
  }

  /**
   * ASCII : 通用变量
   */
  public static final String US_ASCII = "US-ASCII";

  /**
   * US_ASCII : 字符集
   */
  public static final Charset CHARSET_US_ASCII = Charset.forName(US_ASCII);

  /**
   * UTF8 : 通用变量
   */
  public static final String UTF8 = "UTF-8";

  /**
   * UTF8 : 字符集
   */
  public static final Charset CHARSET_UTF8 = Charset.forName(UTF8);

  /**
   * GBK : 通用变量
   */
  public static final String GBK = "GBK";

  /**
   * GBK : 字符集
   */
  public static final Charset CHARSET_GBK = Charset.forName(GBK);

  /**
   * ISO859-1 : 通用变量
   */
  public static final String ISO859 = "ISO-8859-1";

  /**
   * ISO859-1 : 字符集
   */
  public static final Charset CHARSET_ISO859 = Charset.forName(ISO859);

  /**
   * UNICODE : 通用变量
   */
  public static final String UNICODE = "UNICODE";

  /**
   * UNICODE : 字符集
   */
  public static final Charset CHARSET_UNICODE = Charset.forName(UNICODE);

  /**
   * 数据库升序
   */
  public static final String ASC = "asc";

  /**
   * 数据库降序
   */
  public static final String DESC = "desc";

  /**
   * 普通排序
   */
  public static final String ORDERTEMPLETE = " `%s` %s ";
  /**
   * 汉字排序
   */
  public static final String ORDERTEMPLETE_GBK = " convert(`%s` using gbk) collate gbk_chinese_ci %s ";

  /**
   * 数据库错误信息提示，适用于MessageFormat.format
   */
  public static final String DB_ERR_READ = "数据库错误，无法读取数据";

  /**
   * 数据库错误信息提示，适用于MessageFormat.format
   */
  public static final String DB_ERR_SAVE = "数据库错误，无法保存数据";

  public static final String SERVICE_ERR = "系统错误";

  /**
   *   图片占位符
   */
  public static final String REPLACE_IMG = "[图片]";
  /**
   * 空集合的JSON
   */
  public static final String EMPTY_LIST_JSON = "{\"current\":1,\"rowCount\":10,\"rows\":[],\"total\":0}";

  /**
   * 未登录JSON
   */
  public static final String NOT_LOGIN = "{\"success\":false,\"code\":401,\"message\":\"您没有登录或登录超时，请重新登录。（错误码：401）\"}";

  /**
   * 未授权JSON
   */
  public static final String UNAUTHORIZED = "{\"success\":false,\"code\":401,\"message\":\"您没有权限访问这个功能。（错误码：401）\"}";

  /**
   * Web通用的保存用户Session的Name
   */
  public static final String SESSIONUSER = "USER_SESSION";

  /**
   * Web通用的保存用户名Session的Name
   */
  public static final String SESSIONUSERNAME = "USER_SESSION_NAME";

  /**
   * Web通用的保存用户强制下线的信息
   */
  public static final String SESSIONKICKOUT = "USER_SESSION_KICKOUT";

  /**
   * HTTP200 : 成功
   */
  public static final int HTTP200 = HttpServletResponse.SC_OK;

  /**
   * HTTP400 : 请求出错
   */
  public static final int HTTP400 = HttpServletResponse.SC_BAD_REQUEST;

  /**
   * HTTP401 : 未授权
   */
  public static final int HTTP401 = HttpServletResponse.SC_UNAUTHORIZED;

  /**
   * HTTP403 : 禁止访问
   */
  public static final int HTTP403 = HttpServletResponse.SC_FORBIDDEN;

  /**
   * HTTP404 :  资源未找到
   */
  public static final int HTTP404 = HttpServletResponse.SC_NOT_FOUND;

  /**
   * HTTP405 :  不允许此方法
   */
  public static final int HTTP405 = HttpServletResponse.SC_METHOD_NOT_ALLOWED;

  /**
   * HTTP406 : 不可接受
   */
  public static final int HTTP406 = HttpServletResponse.SC_NOT_ACCEPTABLE;

  /**
   * HTTP415 : 不支持的媒体类型
   */
  public static final int HTTP415 = HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE;

  /**
   * HTTP500 :服务器的内部错误
   */
  public static final int HTTP500 = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;

  /**
   * 每天分钟数 24 * 60
   */
  public static final long MM_IN_DAY = 1440L;

  /**
   * 每天秒数 24 * 60 * 60
   */
  public static final long SS_IN_DAY = 86400L;

  /**
   * 每小时秒数 60 * 60
   */
  public static final long SS_IN_HOUR = 3600L;

  /**
   * 每天毫秒数 24 * 60 * 60 * 1000
   */
  public static final long SSS_IN_DAY = 86400000L;

  /**
   * 每小时毫秒数 60 * 60 * 1000
   */
  public static final long SSS_IN_HOUR = 3600000L;

  /**
   * 每分钟毫秒数 60 * 1000
   */
  public static final long SSS_IN_MINUTES = 60000L;

  /**
   * 默认分页数
   */
  public static final int DEFAULT_PAGE_SIZE = 20;

  /**
   * 空字符串
   */
  public static final String EMPTY = StringUtils.EMPTY;

  /**
   * 字符串：“0”
   */
  public static final String ZERO = "0";

  /**
   * 分隔符，字符常量“,”
   */
  public static final char COMMA = ',';

  /**
   * 分隔符，字符串常量“,”
   */
  public static final String COMMAS = ",";

  /**
   * 分隔符，字符常量“_”
   */
  public static final char UNDERLINE = '_';

  /**
   * 分隔符，字符串常量“_”
   */
  public static final String UNDERLINES = "_";

  /**
   * BASE62 : 数字和大小写字母
   */
  public static final String BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

  /**
   * HTML : HTML的ContendType类型
   */
  public static final String HTML = "text/html;charset=UTF-8";

  /**
   * JSON : JSON文件的ContendType类型
   */
  public static final String JSON = "application/json;charset=UTF-8";

  /**
   * 获取img标签正则
   */
  public static final Pattern REG_IMAGE = Pattern.compile("<img.*src\\s*=[\"\\s]*.*?[^>]*?>",
      Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

  /**
   * 获取img标签正则
   */
  public static final Pattern REG_IMAGE_WITH_HTTP = Pattern.compile(
      "<img.*src\\s*=[\"\\s]*http.*?[^>]*?>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

  /**
   * 获取src路径的正则
   */
  public static final Pattern REG_IMAGESRC = Pattern.compile("src\\s*=[\"\\s]*(.*?)(\"|>|\\s+)",
      Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

  /**
   * 身份证18位的正则
   */
  public static final Pattern REG_IDCARD = Pattern.compile("^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|31)|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}([0-9]|x|X)$",
      Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

  /**
   * 中国手机号11位的正则
   */
  public static final Pattern REG_TELEPHONE = Pattern.compile("^1[34578]\\d{9}$",
      Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);

  /**
   * HTTP Status Code
   */
  public static final String SERVLET_STATUS = "javax.servlet.error.status_code";

  /**
   * Servlet Exception
   */
  public static final String SERVLET_EXCEPTION = "javax.servlet.error.exception";

  /**
   * 日期格式
   */
  public static final String DATE_FORMAT1 = "yyyyMMddHHmmss";

  /**
   * 日期格式
   */
  public static final String DATE_FORMAT2 = "yyyy-MM-dd HH:mm:ss";

  /**
   * 日期格式
   */
  public static final String DATE_FORMAT3 = "yyyy-MM-dd";

  /**
   * 未知
   */
  public static final String UNKNOWN = "unknown";

  /**
   * 未删除标识位
   */
  public static final String UNLOCKED = "1";

  /**
   * 删除标识位
   */
  public static final String LOCKED = "0";

  /**
   * 未删除标识位
   */
  public static final String YES = "1";

  /**
   * 删除标识位
   */
  public static final String NO = "0";

  /**
   * 空字符串数组
   */
  public static final String[] EMPTYARRAY = new String[0];

  /**
   * BASE62 : 数字和大小写字母
   */
  private static final char[] BASE62_ARR = BASE62.toCharArray();

  /**
   * 星期
   */
  private static final String[] WEEKDAYS = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

  /**
   * 获取Base62字符组
   * @return Base62字符组
   */
  public static char[] getBase62Arr() {
    return BASE62_ARR;
  }

  /**
   * 获取星期的字符串组
   * @return 星期的字符串组
   */
  public static String[] getWeekDays() {
    return WEEKDAYS;
  }
}
